var express = require('express')
var router = express.Router()
var { saoModel, publicModel } = require("../model/lmymodel");
var multiparty = require('multiparty')
// 发布闲置
router.post('/add', async (req, res) => {
    await publicModel.create(req.body)
    res.send({
        code: 200,
        msg: '添加成功'
    })
})
// 图片上传
router.post('/upload', async (req, res) => {
    let body = req.body
    console.log(body);
    let form = new multiparty.Form()
    form.uploadDir = 'lmyUpload'  //指定上传文件的保存目录
    form.parse(req, async (err, a, files) => {
        if (err) {
            return res.status(400).send('error uploading files')
        }
        // 处理上传的文件
        console.log(files);

        res.send({
            code: 200,
            // img:'http://localhost:3000/'+files.avatar[0].path
        })
    })
})

// 修改图片状态
router.put('/editstatus', async (req, res) => {
    let { _id, status } = req.query
    console.log(_id, status);
    await publicModel.updateOne({ _id }, { status })
    res.send({
        code: 200,
        msg: '修改成功'
    })
})

module.exports = router;